/*
 * Copyright 2012 Andrea Parodi
 *
 *    Licensed under the Apache License, Version 2.0 (the "License");
 *    you may not use this file except in compliance with the License.
 *    You may obtain a copy of the License at
 *
 *        http://www.apache.org/licenses/LICENSE-2.0
 *
 *    Unless required by applicable law or agreed to in writing, software
 *    distributed under the License is distributed on an "AS IS" BASIS,
 *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *    See the License for the specific language governing permissions and
 *    limitations under the License.
 *//*



package eban.storebyrules;


import eban.storebyrules.results.Result;
import eban.storebyrules.results.ResultsetAdapter;
import lombok.extern.log4j.Log4j;

import java.sql.*;

@Log4j
public abstract class JdbcStorage extends AbstractStorage {
    public String dbLocation;

    public JdbcStorage(String dbLocation) {
        super();
        this.dbLocation = dbLocation;
        try {
            Class.forName(getDriverClassName());
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }

    }

    protected abstract String getDriverClassName();



    protected abstract String getConnectionUrl();

    @Override public void dispose() {
        try {
            conn.close();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    @Override protected AbstractTransaction buildTransaction()throws Exception{
        return new AbstractTransaction(this) {};
    }

    @Override protected void commitInternal() throws Exception{
        conn.commit();
    }

    @Override protected void rollbackInternal()throws Exception {
        conn.rollback();
    }
    @Override protected void executeInternal(String sql)  {
        Statement stat=null;
        try {
            stat = conn.createStatement();
            stat.execute(sql);
        } catch (SQLException e) {
            throw new PersistenceException("Cannot execute sql "+sql,e);
        } finally{
            try {
                stat.close();
            } catch (SQLException e) {

            }
        }
    }

    @Override protected Result getResult(String sql) {
        try {
            ResultSet rs = null;

            Statement stat = conn.createStatement();

            rs = stat.executeQuery(sql);

            return new ResultsetAdapter(rs,stat);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    private Connection conn = null;

    @Override protected void createConnectionToNewOrExistingStorage() {
        if (conn!=null) return;
        try {
            if (JdbcStorage.log.isDebugEnabled())
                JdbcStorage.log.debug("Connecting to database");

            conn = getConnection();
            conn.setAutoCommit(false);
            if (!conn.isValid(200))
                throw new PersistenceException("Cannot connect to database");

            if (JdbcStorage.log.isDebugEnabled())
                JdbcStorage.log.debug("Connected to database");


        } catch (SQLException e) {
            throw new RuntimeException(e);
        }

    }

    protected Connection getConnection() throws SQLException {
        return DriverManager.getConnection(getConnectionUrl());
    }


    private final static boolean DEBUG = true;

}

*/
